package com.hadoop.project.gen.maodaligendata;

import com.hadoop.project.entity.hbase.HBaseUser;
import com.hadoop.project.entity.hbase.Tab;
import com.hadoop.project.entity.hbase.UserLog;
import com.hadoop.project.entity.hbase.Video;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.format.number.PercentStyleFormatter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author alexmaodali
 * @deprecated false
 */
public class Gen2 {
    public static final Integer genLength = 100;
    public static void gen(String tableName) throws Exception {
        Connection conn = HBaseUtils.getConnection();
        Table table = conn.getTable(TableName.valueOf(tableName));

        Integer genVideoLen = 10;
        Integer genUserLen = 10;
        Integer genUserLogLen = 10;

        List<String> tidList = new ArrayList<>();
        List<String> uidList = new ArrayList<>();
        List<String> vidList = new ArrayList<>();
        Map<String, String> vidMapFlip = new HashMap<>();

        // 生成Tab数据
        for (int i = 0; i < X.tabList.length; i++) {
            Tab tab = Tab.builder().tid("tid-" + (i + 1)).tname(X.tabList[i]).build();
            tidList.add(tab.getTid());
            // System.out.println(tab);

            // 插入数据到HBase
            Put put = new Put(Bytes.toBytes(tab.getTid()));
            put.addColumn(Bytes.toBytes("tab"), Bytes.toBytes("tid"), Bytes.toBytes(tab.getTid()));
            put.addColumn(Bytes.toBytes("tab"), Bytes.toBytes("tabName"), Bytes.toBytes(tab.getTname()));
            table.put(put);
        }
        // System.out.println(tidList);

        // 生成User数据
        for (int i = 0; i < genUserLen; i++) {
            HBaseUser user = HBaseUser.builder().uid(X.genUid(10)).nickName(X.genNickName()).sex(X.genSex()).age(X.genAge()).province(X.genProvince()).build();
            uidList.add(user.getUid());
            // System.out.println(user);

            //插入数据到HBase
            Put put = new Put(Bytes.toBytes(user.getUid()));
            put.addColumn(Bytes.toBytes("user"), Bytes.toBytes("uid"), Bytes.toBytes(user.getUid()));
            put.addColumn(Bytes.toBytes("user"), Bytes.toBytes("nickname"), Bytes.toBytes(user.getNickName()));
            put.addColumn(Bytes.toBytes("user"), Bytes.toBytes("sex"), Bytes.toBytes(user.getSex()));
            put.addColumn(Bytes.toBytes("user"), Bytes.toBytes("age"), Bytes.toBytes(user.getAge()));
            put.addColumn(Bytes.toBytes("user"), Bytes.toBytes("province"), Bytes.toBytes(user.getProvince()));
            table.put(put);
        }
        // System.out.println(uidList);

        // 生成Video数据
        for (int i = 0; i < genVideoLen; i++) {
            Video video = Video.builder().vid(X.genVid()).tabs(X.genTabs(tidList)).author(X.genAuthor(uidList)).flip(X.genFlip()).build();
            vidList.add(video.getVid());
            vidMapFlip.put(video.getVid(), video.getFlip());
            // System.out.println(video);

            //插入数据到HBase
            Put put = new Put(Bytes.toBytes(video.getVid()));
            put.addColumn(Bytes.toBytes("video"), Bytes.toBytes("vid"), Bytes.toBytes(video.getVid()));
            put.addColumn(Bytes.toBytes("video"), Bytes.toBytes("tabs"), Bytes.toBytes(video.getTabs()));
            put.addColumn(Bytes.toBytes("video"), Bytes.toBytes("author"), Bytes.toBytes(video.getAuthor()));
            put.addColumn(Bytes.toBytes("video"), Bytes.toBytes("flip"), Bytes.toBytes(video.getFlip()));
            table.put(put);
        }

        // 测试
        // for (String vid : vidMapFlip.keySet()) {
        // System.out.println("vid:" + vid + "            value:" + vidMapFlip.get(vid));
        // }

        // 生成UserLog数据
        for (int i = 0; i < genUserLogLen; i++) {
            // vid是vidList中的随机一个
            // videoTime则是vid对应的flip 比flip小的10~flip的数据
            UserLog userLog = UserLog.builder().uid(X.genAuthor(uidList)).id("userLog-" + (i + 1)).vid(X.genVidInUserLog(vidList)).videoTime(X.genVideoTime(10)).build();
            String flip = vidMapFlip.get(userLog.getVid());
            userLog.setVideoTime(flip);
            // System.out.println(userLog);

            // 插入数据到HBase
            Put put = new Put(Bytes.toBytes("userlog-" + userLog.getId()));
            put.addColumn(Bytes.toBytes("userlog"), Bytes.toBytes("id"), Bytes.toBytes(userLog.getId()));
            put.addColumn(Bytes.toBytes("userlog"), Bytes.toBytes("uid"), Bytes.toBytes(userLog.getUid()));
            put.addColumn(Bytes.toBytes("userlog"), Bytes.toBytes("vid"), Bytes.toBytes(userLog.getVid()));
            put.addColumn(Bytes.toBytes("userlog"), Bytes.toBytes("videoTime"), Bytes.toBytes(userLog.getVideoTime()));
            table.put(put);
        }
    }

    public static void main(String[] args) throws Exception {
        gen("testGen01");
    }
}